﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PyNet
{
    static class ChannelFlags
    {
        public static int IsNormal = 0x00;
        public static int IsLobby = 0x01;
        public static int IsVoid = 0x02;
        public static int IsPrivate = 0x04;
    }

    class clsChannel
    {
        public int ChannelID;
        public string Name;
        public string Message;
        public string MessageAuthor = "ServChanSys";
        public int Flags = 0x00;
        public DateTime CreationDate;
        public List<int> Operators = new List<int>();
        public Dictionary<int, clsClient> Patrons = new Dictionary<int, clsClient>();

        public clsChannel(string name, int flags, int ownerID, int channelID)
        {
            CreationDate = DateTime.Now;
            Flags = flags;
            Name = name;
            ChannelID = channelID;
            if (ownerID > 0)
                Operators.Add(ownerID);
        }

        public bool IsLobby()
        {
            return ((Flags & ChannelFlags.IsLobby) == ChannelFlags.IsLobby);
        }

        public bool IsPrivate()
        {
            return ((Flags & ChannelFlags.IsPrivate) == ChannelFlags.IsPrivate);
        }

        public bool IsVoid()
        {
            return ((Flags & ChannelFlags.IsVoid) == ChannelFlags.IsVoid);
        }

        public void Write(string text)
        {
            Program.GetLogger.Write("DarkOrange", "<" + Name + "> ", "Gray", text);
        }

        public void JoinUser(clsClient user)
        {
            Write("Join: " + user.Username);
            Patrons.Add(user.ClientID, user);
            user.Channel = ChannelID;
            user.SendChatEvent(ChatEvents.JoinedChannel, Name, Flags, 0, Message);
            System.Threading.Thread.Sleep(100);
            SendMassChatEvent(ChatEvents.UserJoin, user.Username, user.StatString(), 0, user.Flags);
        }

        public void PartUser(clsClient user)
        {
            Patrons.Remove(user.ClientID);
            SendMassChatEvent(ChatEvents.UserPart, user.Username, user.StatString(), 0, user.Flags);
        }

        public void TalkUser(string name, string text)
        {
            Program.GetLogger.Write("DarkSlateGray", "<" + name + "> ", "Gray", "[" + Name + "] ", "Black", text);
            SendMassChatEvent(ChatEvents.UserTalk, name, text);
        }

        public void SendMassChatEvent(int id, string username, string data)
        {
            SendMassChatEvent(id, username, data, 0, 0);
        }

        public void SendMassChatEvent(int id, string username, string data, int mod, int flags)
        {
            foreach (int i in Patrons.Keys)
            {
                Patrons[i].SendChatEvent(id, username, data);
            }
        }
    }
}
